using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MallItemCellCommon : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_MallItemCellCommon
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject leftTimesGo { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLeftTimesValue { protected set; get; }
            public UnityEngine.GameObject tagInfoGo { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum tagIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText tagInfo { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum qualityLine { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textItemName { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage icon { protected set; get; }
            public IQIGame.Onigao.Framework.ExRawImage rawIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLimitCount { protected set; get; }
            public UnityEngine.GameObject priceSortRoot { protected set; get; }
            public UnityEngine.GameObject buyConditionGo { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textBuyCondition { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("leftTimesGo", out var __tbv0);
                this.leftTimesGo = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLeftTimesValue", out var __tbv1);
                this.textLeftTimesValue = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("tagInfoGo", out var __tbv2);
                this.tagInfoGo = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("tagIcon", out var __tbv3);
                this.tagIcon = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("tagInfo", out var __tbv4);
                this.tagInfo = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("qualityLine", out var __tbv5);
                this.qualityLine = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textItemName", out var __tbv6);
                this.textItemName = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("icon", out var __tbv7);
                this.icon = __tbv7;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("rawIcon", out var __tbv8);
                this.rawIcon = __tbv8;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLimitCount", out var __tbv9);
                this.textLimitCount = __tbv9;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("priceSortRoot", out var __tbv10);
                this.priceSortRoot = __tbv10;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("buyConditionGo", out var __tbv11);
                this.buyConditionGo = __tbv11;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textBuyCondition", out var __tbv12);
                this.textBuyCondition = __tbv12;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private UIN_MallItemPrice mallItemPrice = null;
        private CfgMallItem cfgMallItem = null;
        private bool isUpdateTimeLimit = false;
        #endregion fields

        #region properties

        protected UIB_UIN_MallItemCellCommon ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MallItemCellCommon();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.mallItemPrice = this.InitChildNode<UIN_MallItemPrice>(this.ui.priceSortRoot);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
        }

        protected override void OnDispose()
        {
            this.mallItemPrice = null;
            this.cfgMallItem = null;
            this.isUpdateTimeLimit = false;
        }

        #region methonds

        protected override void OnUpdate(float dt, float unscaledDt)
        {
            if (this.isUpdateTimeLimit)
            {
                this.RefreshTimeLimitView();
            }
        }

        public void Show(CfgMallItem cfgMallItem)
        {
            this.Show();
            this.cfgMallItem = cfgMallItem;
            this.ui.textItemName.text = this.cfgMallItem.Name;
            this.mallItemPrice.Show(this.cfgMallItem);

            this.ShowIcon();
            this.ShowTagInfo();
            this.ShowTimeLimit();
            this.ShowBuyCondition();
            this.ShowLimitBuyCount();
        }

        private void ShowIcon()
        {
            bool isSingleItem = this.cfgMallItem.ShowItem == MallShowItemType.SingleItem;
            string strAssetPathIcon = this.GetIconAssetPath(isSingleItem);

            this.ui.icon.SetActive(isSingleItem);
            this.ui.rawIcon.SetActive(!isSingleItem);
            if (isSingleItem)
            {
                this.SetImageSprite(this.ui.icon, strAssetPathIcon);
            }
            else
            {
                this.SetRawImageTexture(this.ui.rawIcon, strAssetPathIcon);
            }
        }

        private void ShowTagInfo()
        {
            var tag = this.cfgMallItem.Tag;
            var isTagShow = tag != MallItemTag.None;
            this.ui.tagInfoGo.SetActive(isTagShow);
            if (isTagShow)
            {
                this.ui.tagIcon.SetInt((int)tag);
                this.ui.tagInfo.text = APIMall.GetMallItemTagName(tag);
            }
        }

        private void ShowLimitBuyCount()
        {
            bool isShowLimit = this.cfgMallItem.LimitType != MallLimitBuyCountType.None;
            if (isShowLimit)
            {
                int count = MallModule.Instance.GetBuyCount(this.cfgMallItem.Id);
                this.ui.textLimitCount.text = APIMall.GetStrMallLimitBuyCount(this.cfgMallItem.LimitType, this.cfgMallItem.LimitTimes, count);
            }
            this.ui.textLimitCount.SetActive(isShowLimit);
        }

        private void ShowBuyCondition()
        {
            var isCanBuy = MallModule.Instance.CheckConditionIsCanBuy(this.cfgMallItem);
            this.ui.buyConditionGo.SetActive(!isCanBuy);
            if (!isCanBuy)
            {
                var cfgCondition = TableCenter.condition.Get(this.cfgMallItem.ConditionId);
                this.ui.textBuyCondition.text = cfgCondition.FailDesc;
            }
        }

        private void ShowTimeLimit()
        {
            bool isTimeLimit = false;
            if (this.cfgMallItem.TimeLimitType != MallTimeLimitType.None)
            {
                int seconds = MallModule.Instance.GetMallLimitSeconds(this.cfgMallItem);
                isTimeLimit = seconds > 0;
            }
            this.isUpdateTimeLimit = isTimeLimit;
            this.RefreshTimeLimitView();
        }

        private void RefreshTimeLimitView()
        {
            if (this.isUpdateTimeLimit)
            {
                int seconds = MallModule.Instance.GetMallLimitSeconds(this.cfgMallItem);
                this.ui.textLeftTimesValue.text = APIUIConst.GetDayHoursOrHourMinuteSeconds(seconds);
                this.isUpdateTimeLimit = seconds > 0;
            }
            this.ui.leftTimesGo.SetActive(this.isUpdateTimeLimit);
        }

        private string GetIconAssetPath(bool isSingleItem)
        {
            if (isSingleItem)
            {
                if (this.cfgMallItem.Item.Count <= 0)
                {
                    return string.Empty;
                }
                return TableCenter.item.Get(this.cfgMallItem.Item[0])?.Icon_fullPath;
            }
            return this.cfgMallItem.BG_fullPath;
        }

        #endregion methonds
    }
}
